import unittest

from xlwings import Book

from autoTask.taobao.sycm.goods.findMarketRank import FindMarketRank, FindMarketRankV2, FindMarketRankToSystem
from autoTask.taobao.sycm.goods.findMarketRank.FilterAction import FilterAction
from autoTask.taobao.sycm.goods.findMarketRank.GetDataAction import GetDataAction
from common.logger.log import log_
from common.utils import web_page_util, ExcelUtil


class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.p = web_page_util.create_page(is_refresh=False)

    def test_goto_index(self):
        FindMarketRank(param=self.param, p=self.p).goto_index()
        self.assertTrue(self.p.url.startswith("https://sycm.taobao.com/mc/mq/market_rank?activeKey=item"),
                        msg="跳转至任务首页失败")

    def test_get_data_action(self):
        self.product_list = GetDataAction(category="炒锅", date_cycle='30天')(self.p)
        self.assertTrue(len(self.product_list) > 0, msg="获取市场商品失败")
        for product in self.product_list:
            log_.info(product)

    def test_filter_action(self):
        result_data = FilterAction(
            price_scope=[1, 9999],
        )(data_list=self.product_list)
        for data in result_data:
            log_.info(data)

    def test_master_v2_to_local(self):
        FindMarketRankV2.builder_by_dict(self.p, {
            'searchObjList': [
                {
                    "category": "炒锅",
                    "dateCycle": "7天",
                    "minPrice1": 100,
                    "sortAmountTopR": 50,
                    "sortSearchPeopleTopR": 50,
                    "sortMinUVPrice": 3.0,
                    "sortMinPrice": 3.0,
                    "titleContainStr": [],
                    "titleNotContainStr": [],
                }
            ]
        })()

    def test_master_to_system(self):
        """
        将竞品读取到数据库测试
        :return:
        """
        FindMarketRankToSystem(self.p, "炒锅", "7天")()

    def test_temp(self):
        from autoTask.taobao.sycm.market_rank.item import MarketRankItemWebAction
        from autoTask.taobao.sycm.market_rank import util
        MarketRankItemWebAction(self.p).am_export_merge_conversion(7, util.get_temp_file_path())

    def test_excel(self):
        xw_app = ExcelUtil.xw_create_wps_app()
        bk: Book = xw_app.books.add()


if __name__ == '__main__':
    unittest.main()
